package com.ruoyi.merchant.serviceDetails.service.impl;

import java.util.List;
import java.util.ArrayList;
import java.util.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.merchant.serviceDetails.mapper.ServiceDetailsMapper;
import com.ruoyi.merchant.serviceDetails.domain.ServiceDetails;
import com.ruoyi.merchant.serviceDetails.service.IServiceDetailsService;
import com.ruoyi.merchant.shopServiceType.mapper.ShopServiceTypeMapper;
import com.ruoyi.merchant.serviceType.domain.ServiceType;
import com.ruoyi.merchant.serviceType.mapper.ServiceTypeMapper;

/**
 * 服务详情Service业务层处理
 * 
 * @author zzr
 * @date 2025-09-14
 */
@Service
public class ServiceDetailsServiceImpl implements IServiceDetailsService 
{
    @Autowired
    private ServiceDetailsMapper serviceDetailsMapper;

    @Autowired
    private ShopServiceTypeMapper shopServiceTypeMapper;

    @Autowired
    private ServiceTypeMapper serviceTypeMapper;

    /**
     * 查询服务详情
     * 
     * @param id 服务详情主键
     * @return 服务详情
     */
    @Override
    public ServiceDetails selectServiceDetailsById(Long id)
    {
        return serviceDetailsMapper.selectServiceDetailsById(id);
    }

    /**
     * 查询服务详情列表
     * 
     * @param serviceDetails 服务详情
     * @return 服务详情
     */
    @Override
    public List<ServiceDetails> selectServiceDetailsList(ServiceDetails serviceDetails)
    {
        return serviceDetailsMapper.selectServiceDetailsList(serviceDetails);
    }

    /**
     * 新增服务详情
     * 
     * @param serviceDetails 服务详情
     * @return 结果
     */
    @Override
    public int insertServiceDetails(ServiceDetails serviceDetails)
    {
        // 确保中间表中存在店铺服务类别关联
        ensureShopServiceTypeExists(serviceDetails.getMid(), serviceDetails.getTypeId());
        
        return serviceDetailsMapper.insertServiceDetails(serviceDetails);
    }

    /**
     * 确保店铺服务类别关联存在，如果不存在则创建
     * @param mid 店铺ID
     * @param typeId 服务类别ID
     */
    private void ensureShopServiceTypeExists(Long mid, Long typeId) {
        // 检查关联是否存在
        int count = shopServiceTypeMapper.existsByMidAndTypeId(mid, typeId);
        if (count == 0) {
            // 如果不存在，则创建关联
            shopServiceTypeMapper.insertShopServiceType(mid, typeId);
        }
    }

    /**
     * 修改服务详情
     * 
     * @param serviceDetails 服务详情
     * @return 结果
     */
    @Override
    public int updateServiceDetails(ServiceDetails serviceDetails)
    {
        return serviceDetailsMapper.updateServiceDetails(serviceDetails);
    }

    /**
     * 批量删除服务详情
     * 
     * @param ids 需要删除的服务详情主键
     * @return 结果
     */
    @Override
    public int deleteServiceDetailsByIds(Long[] ids)
    {
        return serviceDetailsMapper.deleteServiceDetailsByIds(ids);
    }

    /**
     * 删除服务详情信息
     * 
     * @param id 服务详情主键
     * @return 结果
     */
    @Override
    public int deleteServiceDetailsById(Long id)
    {
        return serviceDetailsMapper.deleteServiceDetailsById(id);
    }

    @Override
    public List<ServiceType> getShopServiceTypes(Long shopId) {
        // 1. 先获取店铺可用的服务类别ID列表
        List<Long> typeIds = shopServiceTypeMapper.getShopServiceTypeIds(shopId);
        
        if (typeIds.isEmpty()) {
            return new ArrayList<>(); // 返回空列表
        }
        
        // 2. 根据ID列表查询服务类别详情
        return serviceTypeMapper.selectServiceTypesByIds(typeIds.toArray(new Long[0]));
    }

    @Override
    public List<ServiceDetails> selectServiceDetailsByShopAndType(Long shopId, Long typeId) {
        // 1. 先校验中间表，确认店铺有权限使用该服务类别
        int count = shopServiceTypeMapper.existsByMidAndTypeId(shopId, typeId);
        if (count == 0) {
            return new ArrayList<>(); // 无权限，返回空列表
        }
        
        // 2. 如果有权限，查询服务详情
        ServiceDetails queryParam = new ServiceDetails();
        queryParam.setMid(shopId);
        queryParam.setTypeId(typeId);
        return serviceDetailsMapper.selectServiceDetailsList(queryParam);
    }

    /**
     * 根据店铺ID查询该店铺的所有服务详情
     * 
     * @param shopId 店铺ID
     * @return 服务详情列表
     */
    public List<ServiceDetails> selectServiceDetailsByShopId(Long shopId) {
        return serviceDetailsMapper.selectServiceDetailsByShopId(shopId);
    }
}
